package test.my.com.myapplicationtest.MyRadioGroup;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import test.my.com.myapplicationtest.R;

/**
 * Created by ThinkPad on 2015/12/18.
 */
public abstract class MyRadioClickListener implements View.OnClickListener{

    private ImageView mLeft_v;
    private ImageView mRight_v;
    private Context mContext;

    // 控制左右单选选中状态
    private boolean isLeftOrRight;
    // 控制左单选多次点击显示状态
    private boolean isLeftSameClick;
    // 控制右单选多次点击显示状态
    private boolean isRightSameClick;

    public MyRadioClickListener(ImageView mLeft_v, ImageView mRight_v, Context mContext) {
        this.mLeft_v = mLeft_v;
        this.mRight_v = mRight_v;
        this.mContext = mContext;
        setEvent();
    }

    private void setEvent(){
        mLeft_v.setOnClickListener(this);
        mRight_v.setOnClickListener(this);
    }

    public abstract void setOnRadioCheckedListener(int view_id,boolean isChecked);
    @Override
    public void onClick(View v) {
        if(v.getId() == mLeft_v.getId()){
            if (!isLeftSameClick) {
                mLeft_v.setBackgroundResource(R.drawable.duigou);
                setOnRadioCheckedListener(mLeft_v.getId(),true);
                if (!isLeftOrRight) {
                    mRight_v.setBackgroundResource(R.drawable.kongxuan);
                    isLeftOrRight = !isLeftOrRight;
                    isRightSameClick = false;
                }
            } else {
                mLeft_v.setBackgroundResource(R.drawable.kongxuan);
                setOnRadioCheckedListener(mLeft_v.getId(), false);
            }
            isLeftSameClick = !isLeftSameClick;
        }else if(v.getId() == mRight_v.getId()){
            if (!isRightSameClick) {
                mRight_v.setBackgroundResource(R.drawable.duigou);
                setOnRadioCheckedListener(mRight_v.getId(), true);
                if (isLeftOrRight) {
                    mLeft_v.setBackgroundResource(R.drawable.kongxuan);
                    isLeftOrRight = !isLeftOrRight;
                    isLeftSameClick = false;
                }
            } else {
                mRight_v.setBackgroundResource(R.drawable.kongxuan);
                setOnRadioCheckedListener(mRight_v.getId(), false);
            }
            isRightSameClick = !isRightSameClick;
        }

    }
}
